﻿using Project_CPU.GenerateData;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using System.Windows.Forms;


namespace Project_CPU.Controllers
{
    public class CPUController : Controller
    {

        public ActionResult Index() //default action in cpu
        {
            var DateTimeFrom = new DateTime(2014, 1, 1, 0, 0, 0);
            var DateTimeTo = new DateTime(2014, 1, 31, 0, 10, 0);
            string s;
            var tmp = GenerateList(Generator.ReturnStatistics(), DateTimeFrom, DateTimeTo); //generate new List for the first month with 30 middle values
            
            var ListToView = new List<DataToView>();
            DateFormat.DefinitionFormatOfDate(DateTimeFrom, DateTimeTo);
            for (int i = 0; i < 30; i++) // ListToView [ string, value ] = tpm[ date.toString, value]
            {
                if (i % 2 == 0)
                    ListToView.Add(new DataToView(DateFormat.ReturnDateInString(tmp[i].date), tmp[i].value));
                else
                    ListToView.Add(new DataToView("", tmp[i].value));
            }
            ViewBag.Entries = ListToView;

            return View();
        }

        public struct DataToView
        {
            public string date;
            public int value;

            public DataToView(string date, int value)
            {
                this.date = date;
                this.value = value;
            }
        } 

        public int BinarySearch(List<Generator.Data> InputList, DateTime example)
        {
            int first = 0, last = InputList.Count, middle, compare;

            while (first < last)
            {
                middle = first + (last - first) / 2;
                compare = DateTime.Compare(example, InputList[middle].date);

                if (compare == 0) //example == myList[middle].date 
                    return middle;

                if (compare <= 0) //example <= myList[middle].date 
                {
                    last = middle;
                }
                else
                {
                    first = middle + 1;
                }
            }

            return last;
        }

        private List<Generator.Data> GenerateList(List<Generator.Data> InputList, DateTime DateTimeFrom, DateTime DateTimeTo) //generate new List with 30 middle values in interval from DateTimeFrom to DateTimeTo
        {
            int periodSec = (int)((DateTimeTo - DateTimeFrom).TotalSeconds);    //period in seconds
            int id = BinarySearch(InputList, DateTimeFrom);                     // the first numder of element we need

            var OutputList = new List<Generator.Data>();   

            for (int i = 0; i < 30; i++) {
                OutputList.Add(InputList[id + (periodSec * i / 30)]);
            }

            return OutputList;
        }
        
        [HttpPost]
        public ActionResult Index(string from, string to) // action in case we need statistics in special interval
        {
            DateTime DateTimeFrom = DateTime.Now;
            DateTime DateTimeTo = DateTime.Now;
            
            if (DateTime.TryParse(from, out DateTimeFrom) && DateTime.TryParse(to, out DateTimeTo)
                && DateTime.Compare(DateTimeFrom, DateTimeTo) <= 0
                && DateTime.Compare(DateTimeTo, DateTime.Now) <= 0
                && DateTime.Compare(Generator.ReturnStatistics()[0].date, DateTimeFrom) <= 0) //check correct of interval
            {
                ViewBag.message = "Данные введены корректно. Выводим от " + from + " до " + to;

            }
            else{
                 DateTimeFrom = new DateTime(2014, 1, 1, 0, 0, 0);
                 DateTimeTo = new DateTime(2014, 1, 31, 0, 0, 0);
                 ViewBag.message = "Данные введены некорректно. Выводим от " + DateTimeFrom.ToString() + " до " + DateTimeTo.ToString();
            }

            //something
            var tmp = GenerateList(Generator.ReturnStatistics(), DateTimeFrom, DateTimeTo); // new List with 30 middle values from interval
            //something

            //init ListToView [ string, value ] where it = tpm[ date.toString, value]
            var ListToView = new List<DataToView>();
            DateFormat.DefinitionFormatOfDate(DateTimeFrom, DateTimeTo);
            for (int i = 0; i < 30; i++) 
            {
                if ( i % 2 == 0 )
                    ListToView.Add(new DataToView(DateFormat.ReturnDateInString( tmp[i].date), tmp[i].value));
                else
                    ListToView.Add(new DataToView("", tmp[i].value));
            }
            ViewBag.Entries = ListToView;

            return View();
        }


        public ActionResult AjaxDisplay()
        {
            List<int> model = new List<int>();
            int count = GenerateData.Generator.ReturnStatistics().Count;

            for (int i = 0; i < 30; i++)
            {
                model.Add(GenerateData.Generator.ReturnStatistics()[count + i - 30].value);
            }
            return View("Ajax", null, model);
        }

        public ActionResult Now()
        {
            ViewBag.Entries = GenerateData.Generator.ReturnStatistics();
            ViewBag.AllElem = GenerateData.Generator.ReturnStatistics().Count();
            return View();
        }

    }
}
